import {AfterViewInit, Component, OnInit, ViewEncapsulation} from '@angular/core';
import {Router} from '@angular/router';
import {PAGES_MENU} from '../menu';
import {MenuService} from '../../lib/service/admin/menu.service';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss'],
})
export class MenuComponent implements OnInit, AfterViewInit {

  public menus = [];
  public currentItems = {
    expanded: '',
    title: '',
    children: ''
  };

  constructor(private menuService: MenuService, private _router: Router) {
    this.menus = PAGES_MENU;
    /*setInterval(() => {
     console.log('aaa');
     console.log(this.menuService.menus);
     }, 1000);*/
  }

  ngAfterViewInit() {
    /*this.menus = this.menuService.menus;
     console.log(this.menuService.menus);
     console.log('存在');
     this.updateMenu(this._router.url);*/
    // console.log(this.menus);
  }

  ngOnInit() {

    // rel
    if (!this.menuService.menus) {
      this.menuService.eventbus.subscribe(_event => {
          if (_event.type === 2) {
            this.menuService.updateMenu(_event.url);
            this.menus = _event.url;
            this.updateMenu(this._router.url);
            this.menuService.eventbus.emit({
              type: 1,
              url: this._router.url
            });
          }
        }
      );
    } else {
      this.menus = this.menuService.menus;
      this.updateMenu(this._router.url);
    }


    /*this.menus = PAGES_MENU;
     this.updateMenu(this.menus);*/


    /*if (!this.menuService.menus) {
     this.menuService.getMenu().subscribe(res => {
     this.menuService.updateMenu(res.data);
     console.log(res.data);
     this.menus = this.menuService.menus;
     this.updateMenu(this._router.url);
     this.menuService.eventbus.emit({
     type: 1,
     url: this._router.url
     });
     });
     console.log('加载');
     } else {
     this.menus = this.menuService.menus;
     console.log('存在');
     this.updateMenu(this._router.url);
     }*/

    /*this.menus = this.menuService.menus;
     console.log(this.menuService.menus);
     this.updateMenu(this._router.url);*/


    // console.log(this.menus);
    // console.log(this.menuService.menus);

    // this.menuItems = this.menuService.menuItems;
    // this._router.isActive()
    // this.updateMenu(this._router.url);
    // this.toggleChildren(this._router.url);
    // console.log('this.currentItems');
    // console.log(this.currentItems);
  }

  public toggle(path: string) {
    this.updateMenu(path);
  }

  public toggleChildren(path: string) {
    // console.log('toggleChildren');
    // console.log(path);
    const arrUrl = path.split('/');
    // console.log(arrUrl);

    this.menus.forEach((items) => {
      items.forEach((item) => {
        // item.data.menu.expanded = false;
        // item.data.menu.selected = false;
        if (arrUrl[1] + '/' + arrUrl[2] === item.path) {
          // item.data.menu.expanded = true;
          // item.data.menu.selected = true;
          // console.log(item);
          item.children.forEach((ite) => {
            if (arrUrl[3] === ite.path) {

              if (ite.expanded === 'true') {
                ite.expanded = 'false';
              } else {
                ite.expanded = 'true';
              }


              // ite.data.menu.expanded = !(ite.data.menu.expanded);
              // ite.data.menu.selected = !(ite.data.menu.selected);
            }
          });
          this.currentItems = item;
          /*item.data.menu.expanded = true;
           item.data.menu.selected = true;
           this.currentItems = item;*/
        }
      });
    });

    // console.log(this.menus);


  }

  public updateMenu(path: string) {
    const arrUrl = path.split('/');
    this.menus.forEach((value) => {
      // 广告＼营销

      value.forEach((value1) => {
        value1.selected = 'false';
        if (arrUrl[1] + '/' + arrUrl[2] === value1.path) {
          value1.selected = 'true';
          this.currentItems = value1;
          if (value1.expanded === 'false') {
            this._router.navigate([path]);
          }
          value1.children.forEach((value2) => {
            if (arrUrl[3] === value2.path) {
              value2.expanded = 'true';
              if (value2.selected === 'true') {
                value2.selected = 'false';
              } else {
                value2.selected = 'true';
              }
            }
          });

        }
      });
    });

    // console.log(this.currentItems);

    /*this.menuItems.forEach((items) => {
     if (!items) {
     return;
     }
     items.forEach((item) => {
     // item.data.menu.expanded = false;
     item.data.menu.selected = false;
     if (arrUrl[1] === item.path) {
     // item.data.menu.expanded = true;
     item.data.menu.selected = true;
     this.currentItems = item;
     if (item.data.menu.expanded === false) {
     this._router.navigate([path]);
     }
     if (!item.children) {
     return;
     }
     item.children.forEach((ite) => {
     if (arrUrl[2] === ite.path) {
     ite.data.menu.expanded = true;
     ite.data.menu.selected = !(ite.data.menu.selected);
     }
     });

     }
     });
     });*/
  }
}
